package com.xiehua.task.client;

import com.xiehua.task.job.pool.FixedJob;
import com.xiehua.task.msg.BindClientBody;
import com.xiehua.task.msg.FixedTaskMetaData;
import com.xiehua.task.prop.XiehuaConfig;
import com.xiehua.task.protocol.TaskPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.utils.json.Json;
import scala.Tuple3;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.xiehua.task.prop.XiehuaConfig.KEY_CLIENT_TOPIC;


/**
 * @author Mr.zhang
 * 2017年3月26日 下午8:20:51
 */
public class ClientListener implements ClientAioListener {

    private static Logger log = LoggerFactory.getLogger(ClientListener.class);


    public static void main(String[] args) {

    }

    /**
     * @author tanyaowu
     */
    public ClientListener() {
    }

    /**
     * @param channelContext
     * @param throwable
     * @param remark
     * @param isRemove
     * @throws Exception
     * @author tanyaowu
     */
    @Override
    public void onAfterClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {
        log.info("onAfterClose channelContext:{}, throwable:{}, remark:{}, isRemove:{}", channelContext, throwable, remark, isRemove);
    }

    /**
     * @param channelContext
     * @param isConnected
     * @param isReconnect
     * @throws Exception
     * @author tanyaowu
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        log.info("客户端连接事件~:{}, isConnected:{}, isReconnect:{}", channelContext, isConnected, isReconnect);
        if (isConnected || isReconnect) {
            // bind client
            BindClientBody bindClientBody = new BindClientBody();
            bindClientBody.setTopicName(XiehuaConfig.getInstance().get(KEY_CLIENT_TOPIC));
            //scan fixed task
            List<FixedJob> fixedJobs =  ClientApp.fixedjob();
            if(fixedJobs != null && fixedJobs.size() > 0){
                bindClientBody.setFixedTaskMetaData(fixedJobs.stream().map(s -> {
                    FixedTaskMetaData t = new FixedTaskMetaData();
                    t.setClassName(s.getClassName());
                    t.setMethodName(s.getMethodName());
                    t.setCron(s.getCron());
                    return t;
                }).collect(Collectors.toList()));
            }
            Aio.send(channelContext, TaskPacket.bindClientPacket(Json.toJson(bindClientBody)));

        }
    }


    /**
     * @param channelContext
     * @param packet
     * @param packetSize
     * @throws Exception
     * @author tanyaowu
     */
    @Override
    public void onAfterReceived(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
        log.info("onAfterReceived channelContext:{}, packet:{}, packetSize:{}", channelContext, Json.toJson(packet), packetSize);
    }

    /**
     * @param channelContext
     * @param packet
     * @param isSentSuccess
     * @throws Exception
     * @author tanyaowu
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
        log.info("onAfterSent channelContext:{}, packet:{}, isSentSuccess:{}", channelContext, Json.toJson(packet), isSentSuccess);
    }

    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {

    }


}
